#include "NeoCalendarDelegate.h"
#include <QAbstractItemView>
#include <QDate>
#include <QPainter>

NeoCalendarDelegate::NeoCalendarDelegate(QObject *parent)
    : QStyledItemDelegate(parent), mDisplayMode(NeoCalendarType::DayView), mIsTransparent(false)
{
}

NeoCalendarDelegate::~NeoCalendarDelegate() = default;

void NeoCalendarDelegate::setDisplayMode(NeoCalendarType::DisplayMode mode)
{
    if (mDisplayMode != mode)
    {
        mDisplayMode = mode;
    }
}

void NeoCalendarDelegate::setTransparent(bool transparent)
{
    if (mIsTransparent != transparent)
    {
        mIsTransparent = transparent;
    }
}

void NeoCalendarDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    if (!mIsTransparent)
    {
        painter->save();
        painter->setPen(Qt::NoPen);
        painter->setBrush(Qt::NoBrush);
        painter->setRenderHint(QPainter::Antialiasing, true);
        painter->setRenderHint(QPainter::TextAntialiasing, true);
        QDate date = index.data(Qt::DisplayRole).value<QDate>();
        QRect rect = option.rect.adjusted(NEO_RADIUS_ADJUSTED_VALUES);
        switch (mDisplayMode)
        {
        case NeoCalendarType::DayView:
        {
            if (date == QDate::currentDate())
            {
                painter->setBrush(QColor(0, 122, 204));
            }
            else if (option.state & QStyle::State_MouseOver)
            {
                painter->setBrush(QColor(200, 200, 200, 128));
            }
            painter->drawEllipse(rect.center(), rect.width() / 2, rect.height() / 2);
            painter->setPen(Qt::black);
            painter->drawText(rect, Qt::AlignCenter, date.toString("dd"));
            break;
        }
        case NeoCalendarType::MonthView:
        {
            if (date.month() == QDate::currentDate().month() && date.year() == QDate::currentDate().year())
            {
                painter->setBrush(QColor(0, 122, 204));
            }
            else if (option.state & QStyle::State_MouseOver)
            {
                painter->setBrush(QColor(200, 200, 200, 128));
            }
            painter->drawEllipse(rect.center(), rect.width() / 2, rect.height() / 2);
            painter->setPen(Qt::black);
            painter->drawText(rect, Qt::AlignCenter, date.toString("MM"));
            break;
        }
        case NeoCalendarType::YearView:
        {
            if (date.year() == QDate::currentDate().year())
            {
                painter->setBrush(QColor(0, 122, 204));
            }
            else if (option.state & QStyle::State_MouseOver)
            {
                painter->setBrush(QColor(200, 200, 200, 128));
            }
            painter->drawEllipse(rect.center(), rect.width() / 2, rect.height() / 2);
            painter->setPen(Qt::black);
            painter->drawText(rect, Qt::AlignCenter, date.toString("yyyy"));
            break;
        }
        default:
            break;
        }
        painter->restore();
    }
}

QSize NeoCalendarDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    QSize size = qobject_cast<const QAbstractItemView *>(option.widget)->viewport()->size();
    switch (mDisplayMode)
    {
    case NeoCalendarType::DayView:
    {
        int extent = qMin(size.width() / mDayShowColumnCount, size.height() / mDayShowRowCount);
        if (size.width() % mDayShowColumnCount == 0 || size.height() % mDayShowRowCount == 0)
        {
            extent -= NEO_RADIUS_ADJUSTED_VALUE;
        }
        return QSize(extent, extent);
    }
    case NeoCalendarType::MonthView:
    {
        int extent = qMin(size.width() / mMonthShowColumnCount, size.height() / mMonthShowRowCount);
        if (size.width() % mMonthShowColumnCount == 0 || size.height() % mMonthShowRowCount == 0)
        {
            extent -= NEO_RADIUS_ADJUSTED_VALUE;
        }
        return QSize(extent, extent);
    }
    case NeoCalendarType::YearView:
    {
        int extent = qMin(size.width() / mYearShowColumnCount, size.height() / mYearShowRowCount);
        if (size.width() % mYearShowColumnCount == 0 || size.height() % mYearShowRowCount == 0)
        {
            extent -= NEO_RADIUS_ADJUSTED_VALUE;
        }
        return QSize(extent, extent);
    }
    default:
        break;
    }
    return QStyledItemDelegate::sizeHint(option, index);
}